package com.myf.warehouse3d.controllers;


import com.myf.authorize.AppUser;
import com.myf.entities.UserEntity;
import com.myf.services.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/test")
public class TestController {
    private final UserService userService;

    public TestController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("hello")
    public String hello() {
        // vXg9dY/B6gcNJH1eQ7Bl2g==
//        return Utility.encrypt("1") + " " + Utility.decrypt("vXg9dY/B6gcNJH1eQ7Bl2g==");
        return "hello";
    }

    @GetMapping("users")
    public List<UserEntity> getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("admin")
    public Optional<UserEntity> getAdmin() {
        return userService.getByCredential("admin", "1");
    }

    @GetMapping("user")
    public String user() {
        var user = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user.account;
    }

    @GetMapping("login")
    public int login() {
        var currentTime = com.myf.Utility.getCurrentUTCTime();
        var map = new HashMap<String, Object>();
        map.put("id", 1);
        map.put("lastLoginAt", currentTime);
        map.put("lastLoginIP", "127.0.0.1");
        map.put("lastUpdateBy", 1);
        map.put("lastUpdateAt", currentTime);
        return userService.updateLastLogin(map);
    }
}